package 力扣;

import java.util.HashMap;

/**
 * @author yyq
 * @create 2022-02-24 19:30
 * 383.救赎金
 * 地址：https://leetcode-cn.com/problems/ransom-note/
 */
public class leetcode383 {
    public static void main(String[] args) {

    }
    public static boolean canConstruct(String ransomNote, String magazine) {
        HashMap<Character,Integer> map=new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            if(map.containsKey(magazine.charAt(i))){
                map.put(magazine.charAt(i),map.get(magazine.charAt(i))+1);
            }
            else {
                map.put(magazine.charAt(i),1);
            }
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            if(map.get(ransomNote.charAt(i))!=null){
                map.put(ransomNote.charAt(i),map.get(ransomNote.charAt(i))-1);
                if(map.get(ransomNote.charAt(i))==0) map.remove(ransomNote.charAt(i));
            }else {
                return false;
            }
        }
        return true;
    }
}
